<!-- /* Description for file.
 * 权限设置
 *
 * @copyright     Copyright 2008-2010, PHInfo Services Inc. (http://www.phinfoservices.com)
 * @package       ues
 * @subpackage    ues/app/views/users
 * @since         ues
 * @version       V0.0.1
 * @modifiedby    张朋2011年0710
 * @lastmodified  张朋2011年0710
 */-->

<?php
	echo $html->css ( 'tree_auth' );
	echo $javascript->link('prototype/prototype-1.6.1');
	echo $javascript->link('prototype/scriptaculous/scriptaculous'); 
	//echo $javascript->link('jquery');
?>
<!-- 二级菜单显示/隐藏 -->
<script type="text/javascript"> 
//	function ExChgClsName(Obj,NameA,NameB){
//		var Obj=document.getElementById(Obj)?document.getElementById(Obj):Obj;
//		Obj.className=Obj.className==NameA?NameB:NameA;
//	}
//	function showMenu(iNo){
//		ExChgClsName("Menu_"+iNo,"MenuBox","MenuBox2");
//	}
</script>
<script type="text/javascript">
	//一级权限修改
	function respondToChange(i) {
		//alert('acoChanged');
		var inputArray = $('subTitle_'+i).childElements();//各<li>
		var controllerCheckId = 'controllerCheck_'+i;
		
		for (var index = 0, len = inputArray.length; index < len; ++index) {
			var item = inputArray[index];
			var inputItems = item.childElements();
			//var inputItem = inputItems.childElements();
			//inputItem.readAttribute('checked')==true?false:true;
			if(index >= (inputArray.length-1))continue;//最后一个是隐藏的input，用来保存allowSubAcoAliasList.见authajax.ctp
			//$(controllerCheckId).checked==false?inputItems[0].checked=false:inputItems[0].checked=true;
			if($(controllerCheckId).checked==false)
			{
				inputItems[0].checked=false;
				$('allowSubAcoAliasList').value = $('allowSubAcoAliasList').readAttribute('value').gsub(','+inputItems[0].value, '');
				$('denySubAcoAliasList').value = $('denySubAcoAliasList').readAttribute('value') + ',' + inputItems[0].value;
			}else{
				inputItems[0].checked=true;
				$('denySubAcoAliasList').value = $('denySubAcoAliasList').readAttribute('value').gsub(','+inputItems[0].value, '');
				$('allowSubAcoAliasList').value = $('allowSubAcoAliasList').readAttribute('value') + ',' + inputItems[0].value;
			}
			//alert(inputItems[0].readAttribute('name'));
		}
		
		//alert($('allowAcoAliasList').readAttribute('value'));
		var allowAcoAliasListValue = $('allowAcoAliasList').readAttribute('value');
		var denyAcoAliasListValue = $('denyAcoAliasList').readAttribute('value');
		if($(controllerCheckId).checked==true)
		{
			$('denyAcoAliasList').value = denyAcoAliasListValue.gsub(','+$(controllerCheckId).readAttribute('name'), '');
			$('allowAcoAliasList').value = allowAcoAliasListValue + ',' + $(controllerCheckId).readAttribute('name');
		}
		else
		{
			$('allowAcoAliasList').value = allowAcoAliasListValue.gsub(','+$(controllerCheckId).readAttribute('name'), '');
			$('denyAcoAliasList').value = denyAcoAliasListValue + ',' + $(controllerCheckId).readAttribute('name');
		}
//		alert('allowAcoAliasList'+$('allowAcoAliasList').readAttribute('value'));
//		alert('denyAcoAliasList'+$('denyAcoAliasList').readAttribute('value'));
//		alert('allowSubAcoAliasList'+$('allowSubAcoAliasList').value);
//		alert('denySubAcoAliasList'+$('denySubAcoAliasList').value);
	}

	//二级权限修改
	function respondToChangeSub(getThis, i){
		//alert(getThis.value);
		//alert($('allowSubAcoAliasList').value);
		if(getThis.checked == true)//选 中二级权限
		{
			$('allowSubAcoAliasList').value = $('allowSubAcoAliasList').readAttribute('value') + ',' + getThis.value;
			$('denySubAcoAliasList').value = $('denySubAcoAliasList').readAttribute('value').gsub(','+getThis.value, '');
		}
		else
		{
			//去掉父checkbox的选中状态及allowAcoAliasList中aco
			var subTitle = Element.ancestors(getThis);
			//alert(subTitle[1].readAttribute('id'));
			var controllerCheckId = subTitle[1].readAttribute('id').gsub('subTitle', 'controllerCheck');
			$(controllerCheckId).checked=false;

			var denyAcoAliasListValue = $('denyAcoAliasList').readAttribute('value');
			$('denyAcoAliasList').value = denyAcoAliasListValue + ',' + $(controllerCheckId).readAttribute('name');
			$('allowAcoAliasList').value = $('allowAcoAliasList').readAttribute('value').gsub(','+$(controllerCheckId).name, '');
			$('allowSubAcoAliasList').value = $('allowSubAcoAliasList').readAttribute('value').gsub(','+getThis.value, '');
			$('denySubAcoAliasList').value = $('denySubAcoAliasList').readAttribute('value') + ',' + getThis.value;

			var inputArray = subTitle[1].childElements();//各<li>
			//var inputArray = $('subTitle_'+i).childElements();//各<li>
			for (var index = 0, len = inputArray.length; index < len; ++index) {
				var item = inputArray[index];
				var inputItems = item.childElements();
				if(index >= (inputArray.length-1))continue;
				if(inputItems[0].checked==true)
					$('allowSubAcoAliasList').value = $('allowSubAcoAliasList').readAttribute('value') + ',' + inputItems[0].value;
				//alert(inputItems[0].readAttribute('name'));
			}
			
		}
//		alert('allowAcoAliasList'+$('allowAcoAliasList').readAttribute('value'));
//		alert('denyAcoAliasList'+$('denyAcoAliasList').readAttribute('value'));
//		alert('allowSubAcoAliasList'+$('allowSubAcoAliasList').value);
//		alert('denySubAcoAliasList'+$('denySubAcoAliasList').value);
	}

	//用户及分组选择框，需要修改$aroAlias
	function respondToChangeUser(getThis, i){
		//alert($('aroAlias').readAttribute('value'));
		var aroAliasValue = $('aroAlias').readAttribute('value');
		if(getThis.checked==true)
			$('aroAlias').value = aroAliasValue + ',' + getThis.name;
		else
		{
			$('aroAlias').value = aroAliasValue.gsub(','+getThis.name, '');
			$('aroAlias').value = aroAliasValue.gsub(getThis.name, '');
		}
		//alert($('aroAlias').readAttribute('value'));
	}
	function respondToChangeGroup(getThis, i){
		//alert($('aroAlias').readAttribute('value'));
		var aroAliasValue = $('aroAlias').readAttribute('value');
		if(getThis.checked==true)
			$('aroAlias').value = aroAliasValue + ',' + getThis.name;
		else
		{
			$('aroAlias').value = aroAliasValue.gsub(','+getThis.name, '');
			$('aroAlias').value = aroAliasValue.gsub(getThis.name, '');
		}
		//alert($('aroAlias').readAttribute('value'));
	}
</script>

<!-- start权限设置表 -->
<?php echo $form->create('User', array('action' => 'auth')); ?>

<!-- 权限列表 -->
<div id="userlist">
	<table width="100%" cellpadding="0" cellspacing="0" class="title" style="table-layout:fixed;">
		<tr>
			<th colspan="2"><?php echo __("User Access")."-".$aroAlias;?></th>
		</tr>
	</table>

	<div class="tree">
		<h3></h3>
		<div class="TreeWrap">
			<?php 
			//echo ($aroAlias);
				$i = 0;
				foreach($acoAliasList as $v){
					$i++;
					
					$checked = "checked";
					if($v['Aco']['checked'] == 1)$checked = 'checked';
					else $checked = 'dischecked';
					
					//if($v['Aco']['alias']=='Resumes' || $v['Aco']['alias']=='Projectmanagements' || $v['Aco']['alias']=='Visitorlists'){
					echo("<div class='MenuBox2' id='Menu_".$i."'>");//class='MenuBox2' 默认隐藏二级菜单
						echo("<div class='titBox' >
							<input id='controllerCheck_".$i."' type='checkbox' onclick='respondToChange(".$i.");' name = '".$v['Aco']['alias']."' ".$checked."/>
							<h3 class='Fst'>"
//							."<a href='javascript:showMenu(".$i.");'>".$v['Aco']['alias']
//							."</a>"
							);
							//echo ($v['Aco']['alias'].','.$aroAlias);
							
							echo $ajax->link(
								    __($v['Aco']['alias'], true), 
								    array( 'controller' => 'users', 'action' => 'authajax', $v['Aco']['alias'].','.$aroAlias ), 
								    array( 'update' => 'subTitle_'.$i , 
								    		'complete' => "$('texBox_".$i."').visible()==true?$('texBox_".$i."').hide():$('texBox_".$i."').show();
								    				$('allowSubAcoAliasList').value= $('allowSubAcoAliasList').readAttribute('value')+','+$('allowSubAcoAliasList_".$v['Aco']['alias']."').readAttribute('value');")
								);
						
						echo("</h3></div>");
						echo("<div class='txtBox' id='texBox_".$i."' style='display: none;'>
							<ul id = 'subTitle_".$i."'>");
//							foreach ($v['Aco']['children'] as $vv){
//								$i++;
//								//echo("<li><a href='#'>"."controllers/".$v['Aco']['alias']."/".$vv['Aco']['alias']."</a></li>");
//								if($vv['Aco']['checked'] == 1)$checked = 'checked';
//								else $checked = 'dischecked';
//								echo("<li><div><input name=\"data[aco][".$i."]\" type='checkbox'".$i." value='"."controllers/".$v['Aco']['alias']."/".$vv['Aco']['alias']."' $checked>".$vv['Aco']['alias']."</div></li>");
//							}
						echo("</ul></div>");
					echo("</div>");
					//}
				}
			?>
		<?php
		//        foreach($aco as $v){
		//        echo "<div>";
		//        echo "<div>".$v['Aco']['alias']."</div>";
		//                foreach($v['Aco']['children'] as $vv){
		//                $cc=$vv['Aco']['checked']?" checked ":"";
		//                        echo "<div><input name=\"data[aco][]\" type='checkbox'".$cc." value='"."controllers/".$v['Aco']['alias']."/".$vv['Aco']['alias']."'>".$vv['Aco']['alias']."</div>";
		//                }
		//        echo "</div>";
		//        }
		?>
		</div>
	</div>
</div>
<div class="right-page">
<!-- 用户选择 -->
<div class='userList-t'>选择用户</div>
<div class='userList'>
	<ul>
	<?php 
		$i = 0;
		foreach ($users as $user){
			$i++;
			if($aroAlias == $user['User']['username'])
				echo("<li><div class='user_".$i."'><input name='".$user['User']['username']."' type='checkbox' value='' checked onclick='respondToChangeUser(this,".$i.");'>".$user['User']['name']."</div></li>");
			else
				echo("<li><div class='user_".$i."'><input name='".$user['User']['username']."' type='checkbox' value='' onclick='respondToChangeUser(this,".$i.");'>".$user['User']['name']."</div></li>");
		}
	?>
	</ul>
</div>

<!-- 用户类别选择 -->
<div class='userGroup-t'>选择用户组</div>
<div class='userGroup'>
	<ul>
	<?php 
		$i = 0;
		foreach ($groups as $group){
			$i++;
			echo("<li><div class='group_".$i."'><input name='".$group['Group']['name']."' type='checkbox' value='' onclick='respondToChangeGroup(this,".$i.");'>".$group['Group']['name']."</div></li>");
		}
	?>
	</ul>
</div>

<?php echo $form->hidden('allowAcoAliasList',array('id'=>'allowAcoAliasList'));?>
<?php echo $form->hidden('denyAcoAliasList',array('id'=>'denyAcoAliasList'));?>
<?php echo $form->hidden('allowSubAcoAliasList',array('id'=>'allowSubAcoAliasList'));?>
<?php echo $form->hidden('denySubAcoAliasList',array('id'=>'denySubAcoAliasList'));?>
<?php echo $form->hidden('aroAlias',array('id'=>'aroAlias','value'=>$aroAlias));?>
<?php echo $form->hidden('nowUserId',array('value'=>$nowUserId));?>

<?php echo $form->button('保存',array('type'=>'submit','class'=>'oksubmit'));?>
<?php echo $form->end();?>
<!-- end权限设置表 -->
</div>
